package au.com.uptick.serendipity.client.view;

import au.com.uptick.serendipity.client.Serendipity;
import au.com.uptick.serendipity.client.data.ResourceCentreDataSource;
import au.com.uptick.serendipity.client.data.SalesDataSource;
import au.com.uptick.serendipity.client.data.SettingsDataSource;
import au.com.uptick.serendipity.client.presenter.MainPageSmartGwtPresenter;
import au.com.uptick.serendipity.client.widgets.ApplicationMenu;
import au.com.uptick.serendipity.client.widgets.Masthead;
import au.com.uptick.serendipity.client.widgets.NavigationPane;
import au.com.uptick.serendipity.client.widgets.NavigationPaneHeader;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;

public class MainPageSmartGwtView extends ViewImpl implements MainPageSmartGwtPresenter.MyView {

    // NORTH_HEIGHT = MASTHEAD_HEIGHT + APPLICATION_MENU_HEIGHT + NAVIGATION_PANE_HEADER_HEIGHT
    private static final int NORTH_HEIGHT = 85;
    private static final int DEFAULT_MENU_WIDTH = 70;
    private static final String DEFAULT_MARGIN = "0px";

    private final Masthead masthead;
    private final ApplicationMenu applicationMenu;
    private final NavigationPaneHeader navigationPaneHeader;
    private final NavigationPane navigationPane;

    private VLayout panel;
    private HLayout northLayout;
    private HLayout southLayout;
    private HLayout eastLayout;
    private VLayout westLayout;

    @Inject
    public MainPageSmartGwtView(Masthead masthead, ApplicationMenu applicationMenu,
                                NavigationPaneHeader navigationPaneHeader, NavigationPane navigationPane) {
        this.masthead = masthead;
        this.applicationMenu = applicationMenu;
        this.navigationPaneHeader = navigationPaneHeader;
        this.navigationPane = navigationPane;

        // get rid of scroll bars, and clear out the window's built-in margin,
        // because we want to take advantage of the entire client area
        Window.enableScrolling(false);
        Window.setMargin(DEFAULT_MARGIN);

        // initialise the main layout container
        panel = new VLayout();
        panel.setWidth100();
        panel.setHeight100();

        // initialise the North layout container
        northLayout = new HLayout();
        northLayout.setHeight(NORTH_HEIGHT);

        initApplicationMenu();

        // initialise the nested layout container
        VLayout vLayout = new VLayout();
        vLayout.addMember(this.masthead);
        vLayout.addMember(this.applicationMenu);
        vLayout.addMember(this.navigationPaneHeader);

        // add the nested layout container to the North layout container
        northLayout.addMember(vLayout);

        initNavigationPane();

        // initialise the West layout container
        westLayout = this.navigationPane;

        // initialise the South layout container
        southLayout = new HLayout();
        eastLayout = new HLayout();

        southLayout.setMembers(westLayout, eastLayout);

        // add the North and South layout containers to the main layout container
        panel.addMember(northLayout);
        panel.addMember(southLayout);
    }

    @Override
    public Widget asWidget() {
        return panel;
    }

    @Override
    public void setInSlot(Object slot, Widget content) {

        Log.debug("setInSlot()");

        if (slot == MainPageSmartGwtPresenter.TYPE_SetContextAreaContent) {
            if (content != null) {
//                southLayout.setMembers(westLayout, (VLayout) content);
                eastLayout.setMembers((VLayout) content);
            }
        } else {
            super.setInSlot(slot, content);
        }
    }

    private void initApplicationMenu() {
        applicationMenu.addMenu(Serendipity.getConstants().NewActivityMenuName(),
                                DEFAULT_MENU_WIDTH, Serendipity.getConstants().NewActivityMenuItemNames(),
                                null);
        applicationMenu.addMenu(Serendipity.getConstants().NewRecordMenuName(),
                                DEFAULT_MENU_WIDTH, Serendipity.getConstants().NewRecordMenuItemNames(),
                                null);
        Menu goToMenu = applicationMenu.addMenu(Serendipity.getConstants().GoToMenuName(), DEFAULT_MENU_WIDTH - 30);
        applicationMenu.addSubMenu(goToMenu, Serendipity.getConstants().SalesMenuItemName(),
                                   Serendipity.getConstants().SalesMenuItemNames(), null);
        applicationMenu.addSubMenu(goToMenu, Serendipity.getConstants().SettingsMenuItemName(),
                                   Serendipity.getConstants().SettingsMenuItemNames(), null);
        applicationMenu.addSubMenu(goToMenu, Serendipity.getConstants().ResourceCentreMenuItemName(),
                                   Serendipity.getConstants().ResourceCentreMenuItemNames(), null);
        applicationMenu.addMenu(Serendipity.getConstants().ToolsMenuName(),
                                DEFAULT_MENU_WIDTH - 30, Serendipity.getConstants().ToolsMenuItemNames(), null);
        applicationMenu.addMenu(Serendipity.getConstants().HelpMenuName(),
                                DEFAULT_MENU_WIDTH - 30, Serendipity.getConstants().HelpMenuItemNames(), null);
    }

    private void initNavigationPane() {
        navigationPane.add(Serendipity.getConstants().SalesStackSectionName(), SalesDataSource.getInstance());
        navigationPane.add(Serendipity.getConstants().SettingsStackSectionName(), SettingsDataSource.getInstance());
        navigationPane.add(Serendipity.getConstants().ResourceCentreStackSectionName(), ResourceCentreDataSource.getInstance());
    }

    public ApplicationMenu getApplicationMenu() {
        return applicationMenu;
    }

    public NavigationPaneHeader getNavigationPaneHeader() {
        return navigationPaneHeader;
    }

    public NavigationPane getNavigationPane() {
        return navigationPane;
    }
}
